package com.example.qsindusty.jmm_spline;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.example.qsindusty.jmm_spline.Config.Config;
import com.example.qsindusty.jmm_spline.ContralFragment.MapFragment;
import com.example.qsindusty.jmm_spline.CreatChildActivity.CreatChildOrder;
import com.example.qsindusty.jmm_spline.HomeFragment.HomeFragment;
import com.example.qsindusty.jmm_spline.InfomationFragment.InfomationFragment;
import com.example.qsindusty.jmm_spline.Jpush.MyReceiver;
import com.example.qsindusty.jmm_spline.MoreSetFramgent.SetFragment;
import com.example.qsindusty.jmm_spline.OrdeFragment.OrderHomeFragment;
import com.example.qsindusty.jmm_spline.OrderinfoActivity.OrderInfoActivity;
import com.example.qsindusty.jmm_spline.SearchActiviity.SearchActivity;

import com.yixin.ystartlibrary.base.BaseAppCompatActivity;
import com.yixin.ystartlibrary.base.PermissionManager;
import com.yixin.ystartlibrary.helper.FragmentHelper;
import com.yixin.ystartlibrary.utils.GeneralPreferencesUtils;
import com.yixin.ystartlibrary.utils.LogUtils;
import com.yixin.ystartlibrary.utils.StatusBarUtil;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;

public class MainActivity extends BaseAppCompatActivity implements MyReceiver.Reveiverlister,OrderInfoActivity.LocationLister {

    @BindView(R.id.main_fragmnet)
    FrameLayout mainFragmnet;
    @BindView(R.id.bottom_navigation_bar_container)
    BottomNavigationBar bottomNavigationBarContainer;
    FragmentHelper fragmentHelper;
    HomeFragment homeFragment ;
    MapFragment controlFragment ;
    OrderHomeFragment orderHomeFragment ;
    InfomationFragment infomationFragment ;

    SetFragment setFragment ;
    ShapeBadgeItem shapeBadgeItem ;
    public  static  int MARK_LOCATION= 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this.getWindow());
        StatusBarUtil.setPaddingSmart(this, mToolBar);
        shapeBadgeItem =new ShapeBadgeItem()
                .setShape(ShapeBadgeItem.SHAPE_OVAL);
        shapeBadgeItem.hide();
            OrderInfoActivity.setLocationLister(this);
        buildbar();
         buildBarLister();
        buildFragmentHepler();
        MyReceiver.setLister(this);
        setLister();
    }

    private  void  setLister(){
        mToolBar.setNavigationIcon(R.mipmap.add);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(mActivity,CreatChildOrder.class);
                startActivity(intent);
            }
        });
        OrderInfoActivity.setLocationLister(this);
        MyReceiver.setLister(this);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    @Override
    protected void init() {
        Set<String> set = new HashSet<>();
        set.add(GeneralPreferencesUtils.getSharedPreference(mActivity, Config.token,null));
        JPushMessage jPushMessage = new JPushMessage();
        int sequence = jPushMessage.getSequence();
        JPushInterface.setTags(getApplicationContext(),sequence,set);

    }


    private void buildFragmentHepler() {
        fragmentHelper = new FragmentHelper(mActivity, getSupportFragmentManager(), R.id.main_fragmnet);
        fragmentHelper.addFragmentItem(new FragmentHelper.FragmentInfo(homeFragment = HomeFragment.newInstance()));
        fragmentHelper.addFragmentItem(new FragmentHelper.FragmentInfo(orderHomeFragment = OrderHomeFragment.newInstance()));
        fragmentHelper.addFragmentItem(new FragmentHelper.FragmentInfo(controlFragment = MapFragment.newInstance()));
        fragmentHelper.addFragmentItem(new FragmentHelper.FragmentInfo(orderHomeFragment = OrderHomeFragment.newInstance()));
        fragmentHelper.addFragmentItem(new FragmentHelper.FragmentInfo(setFragment = SetFragment.newInstance()));
        fragmentHelper.show(HomeFragment.class.getSimpleName());
    }
    private void buildbar() {
        bottomNavigationBarContainer.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBarContainer
                .addItem(new BottomNavigationItem(R.mipmap.menu_seleted ,"配单").setInactiveIconResource(R.mipmap.menu_unseleted).setActiveColorResource(R.color.theme_green))
                .addItem(new BottomNavigationItem(R.mipmap.menu_seleted, "订单").setInactiveIconResource(R.mipmap.menu_unseleted).setActiveColorResource(R.color.theme_green))
                .addItem(new BottomNavigationItem(R.mipmap.control_selted, "监控").setInactiveIconResource(R.mipmap.control_unselted).setActiveColorResource(R.color.theme_green))
                .addItem(new BottomNavigationItem(R.mipmap.infomation_selected, "消息").setInactiveIconResource(R.mipmap.infomation_un).setActiveColorResource(R.color.theme_green).setBadgeItem(shapeBadgeItem))
                .addItem(new BottomNavigationItem(R.mipmap.setting_selted, "更多").setInactiveIconResource(R.mipmap.setting_unselted).setActiveColorResource(R.color.theme_green))
                .initialise();
        bottomNavigationBarContainer.setFirstSelectedPosition(0);
        setCenterTitle("订单");
    }
private  void  selectedPostion(int position){
        LogUtils.i(",,.,./");
        bottomNavigationBarContainer.selectTab(position,true);
}


    private void buildBarLister() {
        if (bottomNavigationBarContainer != null) {
            bottomNavigationBarContainer.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
                @Override
                public void onTabSelected(int position) {
                    switch (position) {

                        case 0:
                            fragmentHelper.show(HomeFragment.class.getSimpleName());
                            setCenterTitle("配单");
                            break;

                        case 1:
                            fragmentHelper.show(OrderHomeFragment.class.getSimpleName());
                            setCenterTitle("订单");
                            break;
                        case 2:

                            if (PermissionManager.getInstance(mActivity).getGrantedInfo(Manifest.permission.ACCESS_FINE_LOCATION)) {
                                fragmentHelper.show(MapFragment.class.getSimpleName());
                                setCenterTitle("监控");

                            } else {
                                PermissionManager.getInstance(mActivity).executeDialog(mActivity, Manifest.permission.ACCESS_FINE_LOCATION,
                                        PermissionManager.getInstance(mActivity).new Builder(mActivity)
                                                .setMessage("应用需要获取您的GPS权限，是否授权？")
                                                .setTitle(getString(R.string.app_name))
                                                .setIcon(R.mipmap.ic_launcher)
                                                .setOk("OK")
                                );
                            }

                            break;
                        case 3:
                            setCenterTitle("消息");
                             fragmentHelper.show(OrderHomeFragment.class.getSimpleName());
                                shapeBadgeItem.hide();
                            break;
                        case 4:
                            setCenterTitle("更多");
                            fragmentHelper.show(SetFragment.class.getSimpleName());
                            break;


                    }
                }

                @Override
                public void onTabUnselected(int position) {

                }

                @Override
                public void onTabReselected(int position) {

                }
            });
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionManager.REQUEST_CODE&&grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
                    selectedPostion(2);
                }else {
                    selectedPostion(0);
                }
            }else{
                selectedPostion(2);
            }
        }else{
            selectedPostion(0);
        }

    }
    private long firstTime = 0;
    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {
            LogUtils.i("再按一次退出程序");
            firstTime = secondTime;
        } else {
            System.exit(0);
        }

    }

    @Override
    public void adddarge() {
        shapeBadgeItem.show();

    }

    @Override
    public void location() {
        selectedPostion(2);
    }
    Menu mMenu;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_search, menu);
        mMenu = menu;
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.search_icon).setVisible(true);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case  R.id.search_icon:
                Intent intent = new Intent(this,SearchActivity.class);
                startActivity(intent);
                break;
        }

        return false;
    }

}
